﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; 
using System.Collections; 
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace WebApplication3.Core.Helper
{
    public class HttpHelper
    {

        private string accept = "application/json,text/javascript,text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
        private System.Net.CookieContainer cc = new System.Net.CookieContainer();
        private string contentType = "application/x-www-form-urlencoded";
        private int currentTry = 0;
        private int delay = 0x3e8;
        public NameValueCollection Heads = new NameValueCollection();
        public bool AllowAutoRedirect = false;
        private System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("utf-8");
        private int maxTry = 300;
        public IWebProxy Proxy;
        private string[] userAgents = new string[] { "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 SE 2.X MetaSr 1.0" };

        public string GetHtml(string url)
        {
            return this.GetHtml(url, this.cc);
        }

        public MemoryStream GetStream(string url)
        {
            return this.GetStream(url, this.cc);
        }

        public string GetHtml(string url, System.Net.CookieContainer cookieContainer)
        {
            return this.GetHtml(url, cookieContainer, url);
        }

        public string UploadFiles(string url, Dictionary<string, string> postDatas, List<HttpUploadingFile> files, string refurl, CookieContainer cookieContainer)
        {

            ServicePointManager.Expect100Continue = false;
            //  if (string.IsNullOrEmpty(postData)) return this.GetHtml(url, cookieContainer);
            this.currentTry++;
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                // byte[] bytes = this.encoding.GetBytes(postData);
                request = (HttpWebRequest)WebRequest.Create(url);
                if (this.Proxy != null) request.Proxy = this.Proxy;
                request.CookieContainer = cookieContainer;
                request.ContentType = this.contentType;
                request.Referer = refurl;
                request.AllowAutoRedirect = AllowAutoRedirect;
                request.Accept = this.accept;
                request.UserAgent = this.userAgent;
                request.Headers.Add(Heads);
                request.Method = "POST";
                Stream requestStream = request.GetRequestStream();
                //requestStream.Write(bytes, 0, bytes.Length);


                MemoryStream memoryStream = new MemoryStream();
                StreamWriter writer = new StreamWriter(memoryStream);

                string newLine = "\r\n";
                string boundary = "NYRpB--RxJI_ZosfQPfveIyIY54h1O";
                request.ContentType = "multipart/form-data; boundary=" + boundary;

                foreach (string key in postDatas.Keys)
                {
                    writer.Write("--" + boundary + newLine);
                    writer.Write("Content-Disposition: form-data; name=\"{0}\"{1}", key, newLine);
                    writer.Write("Content-Type: text/plain; charset=UTF-8{0}{0}", newLine);
                    writer.Write(postDatas[key] + newLine);
                }

                for (int i = 0; i < files.Count; i++)
                {
                    HttpUploadingFile file = files[i];
                    writer.Write("--" + boundary + newLine);
                    writer.Write(
                        "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}",
                        file.FieldName,
                        Path.GetFileName(file.FileName),
                        newLine
                        );
                    writer.Write("Content-Type: application/octet-stream" + newLine + "Content-Transfer-Encoding: binary" + newLine + newLine);
                    writer.Flush();


                    memoryStream.Write(file.Data, 0, file.Data.Length);


                    writer.Write(newLine);
                    if (i == files.Count - 1)
                    {
                        writer.Write("--" + boundary + "--" + newLine);
                    }
                    else
                    {
                        writer.Write("--" + boundary + newLine);
                    }
                }


                writer.Flush();
                memoryStream.Position = 0;
                memoryStream.WriteTo(requestStream);

                request.ContentLength = memoryStream.Length;

                requestStream.Close();


                response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream, this.encoding);

                string str2 = reader.ReadToEnd();
                reader.Close();
                responseStream.Close();
                this.currentTry = 0;
                request.Abort();
                response.Close();
                foreach (Cookie cookie in response.Cookies)
                {
                    cookieContainer.Add(cookie);
                }
                return str2;
            }
            catch (Exception)
            {
                if (request != null) request.Abort();
                if (response != null) response.Close();
                return "";
            }
        }

        public bool CheckValidationResult(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }
        public string GetHtml(string url, System.Net.CookieContainer cookieContainer, string refurl)
        {
            this.currentTry++;
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                request = (HttpWebRequest)WebRequest.Create(url);
                if (this.Proxy != null) request.Proxy = this.Proxy;
                request.CookieContainer = cookieContainer;
                request.ServicePoint.ConnectionLimit = this.maxTry;
                request.Headers.Add(Heads);
                request.Referer = refurl;
                request.Accept = this.accept;
                request.UserAgent = this.userAgent;
                request.AllowAutoRedirect = AllowAutoRedirect;
                request.Method = "GET";

                request.KeepAlive = true;
                response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream, this.encoding);
                string str = reader.ReadToEnd();
                reader.Close();
                responseStream.Close();
                request.Abort();
                response.Close();
                foreach (Cookie cookie in response.Cookies)
                {
                    cookieContainer.Add(cookie);
                }
                return str;
            }
            catch (Exception ex)
            {
                if (request != null) request.Abort();
                if (response != null) response.Close();
                return ex.Message;
            }
        }

        public string GetHtml(string url, string postData, bool isPost)
        {
            try
            {
                return this.GetHtml(url, postData, isPost, this.cc, url);
            }
            catch (Exception ex)
            {
                return "";
            }
        }


        public string GetHtml(string url, string postData, bool isPost, System.Net.CookieContainer cookieContainer, string refurl)
        {
            ServicePointManager.Expect100Continue = false;
            if (string.IsNullOrEmpty(postData)) return this.GetHtml(url, cookieContainer);
            this.currentTry++;
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                byte[] bytes = this.encoding.GetBytes(postData);
                request = (HttpWebRequest)WebRequest.Create(url);
                if (this.Proxy != null) request.Proxy = this.Proxy;
                request.CookieContainer = cookieContainer;
                request.ContentType = this.contentType;
                request.Referer = refurl;
                request.AllowAutoRedirect = AllowAutoRedirect;
                request.Accept = this.accept;
                request.UserAgent = this.userAgent;
                request.Headers.Add(Heads);
                request.Method = isPost ? "POST" : "GET";
                request.ContentLength = bytes.Length;
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream, this.encoding);

                string str2 = reader.ReadToEnd();
                reader.Close();
                responseStream.Close();
                this.currentTry = 0;
                request.Abort();
                response.Close();
                foreach (Cookie cookie in response.Cookies)
                {
                    cookieContainer.Add(cookie);
                }
                return str2;
            }
            catch (Exception)
            {
                if (request != null) request.Abort();
                if (response != null) response.Close();
                return "";
            }
        }

        public MemoryStream GetStream(string url, System.Net.CookieContainer cookieContainer)
        {
            this.currentTry++;
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                if (this.Proxy != null) request.Proxy = this.Proxy;
                request.CookieContainer = cookieContainer;
                request.ContentType = this.contentType;
                request.ServicePoint.ConnectionLimit = this.maxTry;
                request.Timeout = 0x1388;
                request.Referer = url;
                request.Accept = this.accept;
                request.UserAgent = this.userAgent;
                request.Method = "GET";
                response = (HttpWebResponse)request.GetResponse();

                Stream responseStream = response.GetResponseStream();
                this.currentTry--;
                foreach (Cookie cookie in response.Cookies)
                {
                    cookieContainer.Add(cookie);
                }

                MemoryStream stream = new MemoryStream();
                responseStream.CopyTo(stream);
                responseStream.Close();
                return stream;
            }
            catch (Exception ex)
            {
                if (this.currentTry <= this.maxTry) this.GetHtml(url, cookieContainer);
                this.currentTry--;
                if (request != null) request.Abort();
                if (response != null) response.Close();
                return null;
            }
        }


        #region String与CookieContainer互转
        /// <summary>
        /// 将String转CookieContainer
        /// </summary>
        /// <param name="url"></param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public CookieContainer StringToCookie(string url, string cookie)
        {
            string[] arrCookie = cookie.Split(';');
            CookieContainer cookie_container = new CookieContainer();    //加载Cookie
            foreach (string sCookie in arrCookie)
            {
                if (sCookie.IndexOf("expires") > 0)
                    continue;
                cookie_container.SetCookies(new Uri(url), sCookie);
            }
            return cookie_container;
        }

        public string GetCookie()
        {
            using (MemoryStream stream = new MemoryStream())
            {
                IFormatter formatter = new BinaryFormatter();

                formatter.Serialize(stream, cc);
                stream.Position = 0;
                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
                stream.Flush();
                stream.Close();
                return Convert.ToBase64String(buffer);
            }
        }

        public void SetCookie(string str)
        {
            IFormatter formatter = new BinaryFormatter();
            byte[] buffer = Convert.FromBase64String(str);
            MemoryStream stream = new MemoryStream(buffer);
            var obj = (CookieContainer)formatter.Deserialize(stream);
            cc = obj;
            stream.Flush();
            stream.Close();
        }

        /// <summary>
        /// 将CookieContainer转换为string类型
        /// </summary>
        /// <param name="cc"></param>
        /// <returns></returns>
        public string CookieToString()
        {
            System.Collections.Generic.List<Cookie> lstCookies = new System.Collections.Generic.List<Cookie>();
            Hashtable table = (Hashtable)cc.GetType().InvokeMember("m_domainTable",
                System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField |
                System.Reflection.BindingFlags.Instance, null, cc, new object[] { });
            StringBuilder sb = new StringBuilder();
            foreach (object pathList in table.Values)
            {
                SortedList lstCookieCol = (SortedList)pathList.GetType().InvokeMember("m_list",
                    System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField
                    | System.Reflection.BindingFlags.Instance, null, pathList, new object[] { });
                foreach (CookieCollection colCookies in lstCookieCol.Values)
                    foreach (Cookie c in colCookies)
                    {
                        sb.Append(c.Name).Append("=").Append(c.Value).Append(";");
                    }
            }
            return sb.ToString();
        }
        #endregion

        public static void SetHeaderValue(WebHeaderCollection header, string name, string value)
        {
            PropertyInfo property = typeof(WebHeaderCollection).GetProperty("InnerCollection", BindingFlags.NonPublic | BindingFlags.Instance);
            if (property != null)
            {
                NameValueCollection values = property.GetValue(header, null) as NameValueCollection;
                values[name] = value;
            }
        }

        public System.Net.CookieContainer CookieContainer
        {
            get
            {
                return this.cc;
            }
        }

        public System.Text.Encoding Encoding
        {
            get
            {
                return this.encoding;
            }
            set
            {
                this.encoding = value;
            }
        }

        public int MaxTry
        {
            get
            {
                return this.maxTry;
            }
            set
            {
                this.maxTry = value;
            }
        }

        public int NetworkDelay
        {
            get
            {
                Random random = new Random();
                return random.Next(this.delay, this.delay * 2);
            }
            set
            {
                this.delay = value;
            }
        }

        private string userAgent
        {
            get
            {
                return this.userAgents[new Random().Next(0, this.userAgents.Length)];
            }
        }

        #region 发送post请求
        public static string GetPost(string url, Dictionary<string, string> dictionary)
        {
            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            #region 添加Post 参数  
            StringBuilder builder = new StringBuilder();
            foreach (var item in dictionary)
            {
                builder.AppendFormat("{0}={1}&", item.Key, item.Value);
            }
            byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容  
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }
        public static string Post(string requestUrl, string request, int timeOutInSeconds = 60)
        {
            System.GC.Collect();// 

            string res = "";//返回结果

            HttpWebRequest req = null;
            HttpWebResponse rep = null;
            Stream reqStream = null;
            try
            {
                //设置最大连接数
                ServicePointManager.DefaultConnectionLimit = 200;
                /***************************************************************
                * 下面设置HttpWebRequest的相关属性
                * ************************************************************/
                req = (HttpWebRequest)WebRequest.Create(requestUrl);

                req.Method = "POST";
                req.Timeout = timeOutInSeconds * 1000;
                req.ContentType = "application/json";
                byte[] data = System.Text.Encoding.UTF8.GetBytes(request);
                req.ContentLength = data.Length;
                //往服务器写入数据
                reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();

                rep = (HttpWebResponse)req.GetResponse();

                //获取服务端返回数据
                StreamReader sr = new StreamReader(rep.GetResponseStream(), Encoding.UTF8);
                res = sr.ReadToEnd().Trim();
                sr.Close();
            }
            catch (System.Threading.ThreadAbortException e)
            {
                System.Threading.Thread.ResetAbort();
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                }
                throw new Exception(e.ToString());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            finally
            {
                //关闭连接和流
                if (rep != null)
                {
                    rep.Close();
                }
                if (req != null)
                {
                    req.Abort();
                }
            }
            return res;
        }

        /// <summary>
        /// HTTP POST方式请求数据
        /// </summary>
        /// <param name="url">URL.</param>
        /// <param name="param">POST的数据</param>
        /// <returns></returns>
        public static string HttpPost(string url, string param = null)
        {
            HttpWebRequest request;

            //如果是发送HTTPS请求  
            if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult1);
                request = WebRequest.Create(url) as HttpWebRequest;
                request.ProtocolVersion = HttpVersion.Version10;
            }
            else
            {
                request = WebRequest.Create(url) as HttpWebRequest;
            }

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "*/*";
            request.Timeout = 60 * 1000;
            request.AllowAutoRedirect = false;



            StreamWriter requestStream = null;
            WebResponse response = null;
            string responseStr = null;

            try
            {
                requestStream = new StreamWriter(request.GetRequestStream());
                requestStream.Write(param);
                requestStream.Close();

                response = request.GetResponse();
                if (response != null)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    responseStr = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                request = null;
                requestStream = null;
                response = null;
            }

            return responseStr;
        }
        private static bool CheckValidationResult1(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true; //总是接受  
        }

        #endregion

        #region Get
        /// <summary>
        /// HTTP GET方式请求数据.
        /// </summary>
        /// <param name="url">URL.</param>
        /// <returns></returns>
        public static string HttpGet(string url)
        {
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            //request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "*/*";
            request.Timeout = 15000;
            request.AllowAutoRedirect = false;
            WebResponse response = null;
            string responseStr = null;
            try
            {
                response = request.GetResponse();
                if (response != null)
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    responseStr = reader.ReadToEnd();
                    reader.Close();
                }
            }
            catch (Exception e)
            {
                Core.Log.Error(e.Message + "," + url);
            }
            return responseStr;
        }
        #endregion
    }

    public class HttpUploadingFile
    {
        private string fileName;
        private string fieldName;
        private byte[] data;

        public string FileName
        {
            get { return fileName; }
            set { fileName = value; }
        }

        public string FieldName
        {
            get { return fieldName; }
            set { fieldName = value; }
        }

        public byte[] Data
        {
            get { return data; }
            set { data = value; }
        }

        public HttpUploadingFile(string fileName, string fieldName)
        {
            this.fileName = fileName;
            this.fieldName = fieldName;
            using (FileStream stream = new FileStream(fileName, FileMode.Open))
            {
                byte[] inBytes = new byte[stream.Length];
                stream.Read(inBytes, 0, inBytes.Length);
                data = inBytes;
            }
        }

        public HttpUploadingFile(byte[] data, string fileName, string fieldName)
        {
            this.data = data;
            this.fileName = fileName;
            this.fieldName = fieldName;
        }
    }
}